<template>
  <div class="recommend" ref="recommend">
    <scroll ref="scroll" class="recommend-content" :data="hotList">
      <div>
        <div v-if="recommends.length" class="slider-wrapper">
          <slider>
            <div v-for="(item,index) in recommends" :key="index">
              <a :href="item.linkUrl">
                <img class="needsclick" @load="loadImage" :src="item.picUrl"/>
              </a>
            </div>
          </slider>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌曲推荐</h1>
          <ul>
            <li @click="selectItem(item, index)" v-for="(item,index) in hotList" :key="index" class="item">
              <div class="icon">
                <img width="60" height="60" v-lazy="item.pic">
              </div>
              <div class="text">
                <h2 class="name" v-html="item.name"></h2>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!hotList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
import Scroll from "base/scroll/scroll";
import Loading from "base/loading/loading";
import Slider from "base/slider/slider";
import { getRecommend } from "api/recommend";
import { playlistMixin } from 'common/js/mixin'
import {mapActions} from 'vuex'
import {allSong} from "../../api";
import {createSong} from "common/js/song";
import SongList from "../../base/song-list/song-list";

export default {
  mixins: [playlistMixin],
  data() {
    return {
      recommends: [],
      hotList: []
    };
  },
  components: {
    SongList,
    Slider,
    Scroll,
    Loading
  },
  created() {
    this._getRecommend();
    this._getHotSong()
  },
  methods: {
    loadImage() {
        if (!this.checkloaded) {
          this.$refs.scroll.refresh()
          this.checkloaded = true
      }
    },
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.recommend.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    ...mapActions([
      'selectPlay'
    ]),
    selectItem(item, index) {
        this.selectPlay({
          list: this.hotList,
          index
        })
    },
    _getRecommend() {
      getRecommend().then((res) => {
        if (res.ret && res.data) {
          this.recommends = res.data.recommendList
        }
      });
    },
    _getHotSong() {
      let ret = []
      allSong().then((res) => {
        if(res){
          for(let k=0; k<10 ;k++){
            ret[k] = res[10*k]
            //console.log(res[k])
          }
        }
        this.hotList = this._normalizeSong(ret)
      })
    },
    _normalizeSong(list) {
      let ret = []
      list.forEach((musicDate) => {
        if(musicDate.id && musicDate.url) {
          ret.push(createSong(musicDate))
        }
      })
      return ret
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';

.recommend {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;

  .recommend-content {
    height: 100%;
    overflow: hidden;

    .slider-wrapper {
      position: relative;
      width: 100%;
      overflow: hidden;
    }

    .recommend-list {
      .list-title {
        height: 65px;
        line-height: 65px;
        text-align: center;
        font-size: $font-size-medium;
        color: $color-theme;
      }

      .item {
        display: flex;
        box-sizing: border-box;
        align-items: center;
        padding: 0 20px 20px 20px;

        .icon {
          flex: 0 0 60px;
          width: 60px;
          padding-right: 20px;
        }

        .text {
          display: flex;
          flex-direction: column;
          justify-content: center;
          flex: 1;
          line-height: 20px;
          overflow: hidden;
          font-size: $font-size-medium;

          .name {
            margin-bottom: 10px;
            color: $color-text;
          }
        }
      }
    }

    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
</style>
